[VB.Net] System.IO will copy files, but fails to update destinations file attributes

Posted by CFP on Stack Overflow See other posts from Stack Overflow or by CFP
Published on 2010-05-25T21:47:55Z Indexed on 2010/05/25 21:51 UTC
Read the original article Hit count: 391

Filed under:
|
|

Hello,

I have a little vb.net script that will copy a file, set its attributes to Normal, update the file time, and then set back the attributes to match those of the source file.

If IO.File.Exists(Destination) Then IO.File.SetAttributes(Destination, IO.FileAttributes.Normal)
IO.File.Copy(Source, Destination, True)

IO.File.SetAttributes(Destination, IO.FileAttributes.Normal)
IO.File.SetLastWriteTimeUtc(Destination, IO.File.GetLastWriteTimeUtc(Destination).AddHours(1))

IO.File.SetAttributes(Destination, IO.File.GetAttributes(Source))

I however I'm encountering a quite strange problem. On some configurations, IO.File.SetLastWriteTimeUtc triggers an UnauthorizedAccess error, although the IO.File.Copy instruction worked very well.

I'm totally puzzled: I've checked, and file attributes are set to 128 (ie. Normal) successfully. The problem seems to be with the very SetLastWriteTimeUtc. But what is it?

Any ideas?
Thanks a lot!

© Stack Overflow or respective owner

Related posts about security

Related posts about file-io